<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UserTestSeed extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $row = DB::table('test')->get();

        collect($row)->each(function ($item){

            User::create([
                'name'=>$item->name,
                'nickname'  => $item->name,
                'mobile'=>$item->mobile ? $item->mobile : '',
                'identity' => $item->ident ? $item->ident : '',
                'address'   => $item->address ? $item->address : '',
            ]);
        });

        collect($row)->whereNotNull('invite')->each(function ($item){

            if ($invite = User::firstWhere('name',$item->invite)){

                $user = User::where('name',$item->name)->first();

                $user->invite = $invite->id;

                $user->save();
            }


        });
    }
}
